package org.toves.checklua;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import org.toves.checklua.ast.Complaint;

public class FileTest {
	public static void main(String[] args) throws FileNotFoundException {
		if (args.length < 1) {
			System.out.println("Usage: FileTest filename");
			System.exit(1);
		}
		
		File f = new File(args[0]);
		lineByLine(f);
		//wholeThing(f);
	}
	
	public static void lineByLine(File f) throws FileNotFoundException {
		Checker checker = new Checker();
		Scanner in = new Scanner(f);
		while (in.hasNextLine()) {
			String line = in.nextLine();
			System.out.println("Line: '" + line + "'");
			CheckerResult result = checker.process(line);
			for (Complaint c : result.getComplaints()) {
				System.out.println(c.toString());
			}
			if (!result.containsError()) {
				result.commit();
				System.out.println("ok");
			}
		}
	}
	
	public static void wholeThing(File f) throws FileNotFoundException {
		StringBuilder sb = new StringBuilder();
		Scanner in = new Scanner(f);
		while (in.hasNextLine()) {
			sb.append(in.nextLine());
		}
		
		Checker checker = new Checker();
		CheckerResult result = checker.process(sb.toString());
		for (Complaint c : result.getComplaints()) {
			System.out.println(c.toString());
		}
		if (!result.containsError()) {
			result.commit();
			System.out.println("ok");
		}
	}
}
